import os
from PyPDF4 import PdfFileMerger


class PDFToOne:
    def toDO(self,path):
        try:
            files = os.listdir(path)
            pdf_list = [file for file in files if file.endswith(".pdf")]
            pdf_path = [os.path.join(path, filename) for filename in pdf_list]
            print(pdf_path)
            merger = PdfFileMerger()
            for pdf in pdf_path:
                merger.append(pdf)
            output_path = os.path.join(path, "合并pdf.pdf")
            merger.write(output_path)
            merger.close()
            input("合并完毕!")
        except:
            input("路径错误！")


if __name__ == '__main__':
    pdf = PDFToOne()
    path=input("请输入pdf文件夹路径（C:/Users/ACLang/Desktop/pdf）：")
    # path = "C:/Users/ACLang/Desktop/pdf"
    pdf.toDO(path)
